package com.wsd.tank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 	坦克类
 * 
 * @author fengmoboy
 *
 */
public class Tank {
	/**
	 * x坐标
	 */
	private int x = 200;
	/**
	 * y坐标
	 */
	private int y = 200;
	
	private int width = 0;
	
	private int height = 0;
	
	private Random random = new Random();
	
	private Group group = Group.BAD;
	
	private Rectangle rectTank = new Rectangle();

	/**
	 * 	速度
	 */
	private static final int SPEED = PorpertyMgr.build().get("tankSpeed");
	
	/**
	 * 
	 */
	private boolean moving = true;
	
	private TankFrame tf;

	private Direction direction = Direction.DOWN;
	
	private BufferedImage tankImage = null;
	
	private boolean living = true;
	
	private Long conut = 0L;

	public Tank() {
		super();
	}

	public Tank(int x, int y, Direction direction, Group group, TankFrame tf) {
		super();
		this.switchByDir();
		this.x = x;
		this.y = y;
		this.width = tankImage.getWidth();
		this.height = tankImage.getHeight();
		this.rectTank.setLocation(this.x, this.y);
		this.rectTank.setSize(this.width, this.height);
		this.direction = direction;
		this.group = group;
		this.tf = tf;
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Direction getDirection() {
		return direction;
	}

	public void setDirection(Direction direction) {
		this.direction = direction;
	}

	public boolean isMoving() {
		return moving;
	}

	public void setMoving(boolean moving) {
		this.moving = moving;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public Long getConut() {
		return conut;
	}

	public void setConut(Long conut) {
		this.conut = conut;
	}

	public Rectangle getRectTank() {
		return rectTank;
	}

	public void setRectTank(Rectangle rectTank) {
		this.rectTank = rectTank;
	}

	// 绘制坦克
	public void paint(Graphics g) {
		if(!living) {
			tf.tanks.remove(this);
		}
		this.paintTankByDirection(g);
		move();
	}

	// 根据方向获取坦克图片
	private void paintTankByDirection(Graphics g) {
		switchByDir();
		g.drawImage(tankImage, x, y, null);
	}

	private void switchByDir() {
		if(this.conut == 100) {
			this.conut = 0l;
		}
		switch (direction) {
			case LEFT:
				tankImage = Group.GOOD == group ? ResourceMgr.goodTankL1 : ResourceMgr.badTankL1;
				if(this.conut > 50) {
					tankImage = Group.GOOD == group ? ResourceMgr.goodTankL2 : ResourceMgr.badTankL2;
				} 
				break;
			case UP:
				tankImage = Group.GOOD == group ? ResourceMgr.goodTankU1 : ResourceMgr.badTankU1;
				if(this.conut > 50) {
					tankImage = Group.GOOD == group ? ResourceMgr.goodTankU2 : ResourceMgr.badTankU2;
				} 
				break;
			case RIGHT:
				tankImage = Group.GOOD == group ? ResourceMgr.goodTankR1 : ResourceMgr.badTankR1;
				if(this.conut > 50) {
					tankImage = Group.GOOD == group ? ResourceMgr.goodTankR2 : ResourceMgr.badTankR2;
				} 
				break;
			case DOWN:
				tankImage = Group.GOOD == group ? ResourceMgr.goodTankD1 : ResourceMgr.badTankD1;
				if(this.conut > 50) {
					tankImage = Group.GOOD == group ? ResourceMgr.goodTankD2 : ResourceMgr.badTankD2;
				} 
				break;
			default:
				break;
		}
		this.conut++;
	}

	// 坦克移动
	private void move() {
		if(!this.moving) 
			return;
		switch (direction) {
			case LEFT:
				x -= SPEED;
				break;
			case UP:
				y -= SPEED;
				break;
			case RIGHT:
				x += SPEED;
				break;
			case DOWN:
				y += SPEED;
				break;
			default:
				break;
		}
		if(this.group == Group.BAD && random.nextInt(100) > 95) 
			this.fire();
		if(this.group == Group.BAD && random.nextInt(100) > 95)
			randomDir();
			// new Thread(()->new Audio("audio/tank_move.wav").play()).start();
		boundsCheck();
		this.rectTank.setLocation(this.x, this.y);
	}

	private void boundsCheck() {
		if(x < 2) x = 2;
		if(y < 28) y = 28;
		if(x > TankFrame.GAME_WDITH - this.getWidth() - 2) x = TankFrame.GAME_WDITH - this.getWidth() - 2;
		if(y > TankFrame.GAME_HEIGHT - this.getHeight() - 2) y = TankFrame.GAME_HEIGHT - this.getHeight() - 2;
	}

	private void randomDir() {
		this.direction = Direction.values()[random.nextInt(4)];
	}

	// 坦克发火
	public void fire() {
		tf.bullets.add(new Bullet(this.x, this.y, this.direction, this.group, this.tf, this));
		if(Group.GOOD == this.group) new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
	}

	public void die() {
		this.living = false;
		int eX = this.x + this.width/2 - Explode.WIDTH/2;
		int eY = this.y + this.height/2 - Explode.HEIGHT/2;
		tf.explodes.add(new Explode(eX, eY, tf));
	}

	public Rectangle buildRectTank(int x, int y, int width, int height) {
		if(rectTank == null) {
			rectTank = new Rectangle(x, y, width, height);
		} else {
			rectTank.setLocation(x, y);
			rectTank.setSize(width, height);
		}
		return rectTank;
	}
	
}
